home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0187_Making Your Delphi 2.0 Applications "Sin.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-11-29  |  2.8 KB  |  132 lines

  1.  
  2. (*
  3. This TI demonstrates how to make your Delphi 2.0
  4. application "sing" by loading and playing a wave file
  5. four different ways:
  6.  
  7. 1) Use the sndPlaySound() function to directly
  8. play a wave file.
  9.  
  10. 2) Read the wave file into memory, then use the
  11. sndPlaySound() to play the wave file
  12.  
  13. 3) Use sndPlaySound to directly play a wave
  14. file thats embedded in a resource file attached
  15. to your application.
  16.  
  17. 4) Read a wave file thats embedded in a resource
  18.  file attached to your application into memory,
  19.  then use the sndPlaySound() to play the wave file.
  20.  
  21.  To build the project you will need to:
  22.  
  23. 1) Create a wave file called 'hello.wav'
  24. in the project's directory.
  25.  
  26. 2) Create a text file called 'snddata.rc'
  27. in the project's directory.
  28.  
  29. 3) Add the following line to the file 'snddata.rc':
  30. HELLO WAVE hello.wav
  31.  
  32. 4) At a dos prompt, go to your project directory
  33. and compile the .rc file using the Borland Resource
  34. compiler (brcc32.exe) by typing the path to brcc32.exe
  35. and giving 'snddata.rc' as a parameter.
  36.  
  37. Example:
  38.  
  39. bin\brcc32 snddata.rc
  40.  
  41. This will create the file 'snddata.res' that
  42. Delphi will link with your application's .exe
  43. file.
  44.  
  45. Final Note: Keep on Jamm'n!
  46.  
  47. *)
  48.  
  49. unit PlaySnd1;
  50.  
  51. interface
  52.  
  53. uses
  54.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  55.   StdCtrls;
  56.  
  57. type
  58.   TForm1 = class(TForm)
  59.     PlaySndFromFile: TButton;
  60.     PlaySndFromMemory: TButton;
  61.     PlaySndbyLoadRes: TButton;
  62.     PlaySndFromRes: TButton;
  63.     procedure PlaySndFromFileClick(Sender: TObject);
  64.     procedure PlaySndFromMemoryClick(Sender: TObject);
  65.     procedure PlaySndFromResClick(Sender: TObject);
  66.     procedure PlaySndbyLoadResClick(Sender: TObject);
  67.   private
  68.     { Private declarations }
  69.   public
  70.     { Public declarations }
  71.   end;
  72.  
  73. var
  74.   Form1: TForm1;
  75.  
  76. implementation
  77.  
  78. {$R *.DFM}
  79.  
  80. {$R snddata.res}
  81.  
  82. uses MMSystem;
  83.  
  84. procedure TForm1.PlaySndFromFileClick(Sender: TObject);
  85. begin
  86.   sndPlaySound('hello.wav',
  87.                 SND_FILENAME or SND_SYNC);
  88. end;
  89.  
  90. procedure TForm1.PlaySndFromMemoryClick(Sender: TObject);
  91. var
  92.   f: file;
  93.   p: pointer;
  94.   fs: integer;
  95. begin
  96.   AssignFile(f, 'hello.wav');
  97.   Reset(f,1);
  98.   fs := FileSize(f);
  99.   GetMem(p, fs);
  100.   BlockRead(f, p^, fs);
  101.   CloseFile(f);
  102.   sndPlaySound(p,
  103.                SND_MEMORY or SND_SYNC);
  104.   FreeMem(p, fs);
  105. end;
  106.  
  107. procedure TForm1.PlaySndFromResClick(Sender: TObject);
  108. begin
  109.   PlaySound('HELLO',
  110.             hInstance,
  111.             SND_RESOURCE or SND_SYNC);
  112. end;
  113.  
  114. procedure TForm1.PlaySndbyLoadResClick(Sender: TObject);
  115. var
  116.   h: THandle;
  117.   p: pointer;
  118. begin
  119.   h := FindResource(hInstance,
  120.                     'HELLO',
  121.                     'WAVE');
  122.   h := LoadResource(hInstance, h);
  123.   p := LockResource(h);
  124.   sndPlaySound(p,
  125.                SND_MEMORY or SND_SYNC);
  126.   UnLockResource(h);
  127.   FreeResource(h);
  128. end;
  129.  
  130.  
  131. end.
  132.